package com.ssm.comm.session;

import com.ssm.comm.util.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.web.servlet.server.Session;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;

public class MyHttpSessionWrapper implements HttpSession {

    private String sessionId ;
    /**
     *  真正的存储数据
     */
    private Map<String,Object> attributes = new LinkedHashMap<>();
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Map<String,Object> map = new LinkedHashMap<>() ;
    private HttpSession srcSession ;
    /**
     *  分布式Session
     */
    private ShareSessionService shareSessionService;

    public MyHttpSessionWrapper() {
        shareSessionService = SpringContextUtil.getBean(ShareSessionService.class) ;
    }

    public MyHttpSessionWrapper( HttpSession src ){
        this();
        this.srcSession = src ;
    }

    public MyHttpSessionWrapper(HttpServletRequest request , HttpSession src,String sessionId ){
        this(src);
        this.request = request ;
        this.sessionId = sessionId ;
        if(StringUtils.isBlank(this.sessionId)){
            this.sessionId= this.srcSession.getId() ;
        }
    }

    public MyHttpSessionWrapper(String sid ,HttpSession src ,HttpServletRequest req ,HttpServletResponse res ){
         this(req,src,sid) ;
         this.response = res ;
    }


    private Map<String,Object> getSessionMap(){
        if( this.map.size() == 0 ){
            this.map .putAll(shareSessionService.getSession(this.sessionId));
        }
        return this.map ;
    }

    @Override
    public Object getAttribute(String k){
        Object v = getSessionMap().get(k);
        // TODO redis key 续命一小时
        shareSessionService.aaaa(this.sessionId);
        return v;
    }
@Override
    public void setAttribute(String k ,Object v){
      getSessionMap().put(k,v) ;
      shareSessionService.putSession(this.sessionId,this.map);
      this.srcSession.setAttribute(k,v);
    }

    @Override
    public void invalidate(){
        if(null !=map){
            map.clear();
        }
        shareSessionService.removeRedisSession(this.sessionId);
        this.srcSession.invalidate();
        sessionId = this.srcSession.getId() ;
    }

    @Override
    public void removeAttribute(String k){
        getSessionMap().remove(k);
        this.srcSession.removeAttribute(k);
        shareSessionService.putSession(this.sessionId,this.map);
    }

    @Override
    public Object getValue(String s){return this.srcSession.getValue(s);}

    @Override
    public Enumeration<String> getAttributeNames(){
        Vector<String> s = new Vector<>( );
        if(map.size()!=0){
            s.addAll(map.keySet());
        }
        return s.elements();
    }
    @Override
    public String[] getValueNames(){return this.srcSession.getValueNames();}
    @Override
    public void putValue(String s,Object v){this.srcSession.putValue(s,v);}
    @Override
    public void removeValue(String s){this.srcSession.removeValue(s);}

    @Override
    public long getCreationTime() {
        return this.srcSession.getCreationTime() ;
    }

    @Override
    public String getId(){return this.sessionId ;}
    @Override
    public long getLastAccessedTime(){return this.srcSession.getLastAccessedTime() ;}
    @Override
    public ServletContext getServletContext(){
        return this.srcSession.getServletContext() ;
    }
    @Override
    public void setMaxInactiveInterval(int i){
        this.srcSession.setMaxInactiveInterval(i);
    }
    @Override
    public int getMaxInactiveInterval(){
        return this.srcSession.getMaxInactiveInterval() ;
    }
    @Override
    public HttpSessionContext getSessionContext(){
        return this.srcSession.getSessionContext() ;
    }
    @Override
    public boolean isNew(){
        return this.srcSession.isNew();
    }


}
